import { fileURLToPath, URL } from 'node:url'
import path from 'path'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import setting from "./src/config/setting"
import { createHtmlPlugin } from "vite-plugin-html"
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { createSvgIconsPlugin } from "vite-plugin-svg-icons"

const publicPath = setting.publicPath  // 基础路径
const title = setting.title || 'sakura-web'  // 网站标题

// https://vitejs.dev/config/
export default defineConfig({
  // 配置基础路径
  base: publicPath + '/',
  // 配置需要使用的插件列表
  plugins: [
    vue(),
    // 动态配置网站标题
    createHtmlPlugin({
      inject: {
        data: {
          title: title
        }
      }
    }),
    // 自动导入element-plus组件
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    // 配置svg组件
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
      symbolId: 'icon-[dir]-[name]'
    })
  ],
  // 配置别名
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  // 本地运行配置及代理
  server: {
    host: '0.0.0.0',
    port: 80,
    open: true,
    proxy: {
      '/dev-api': {
        // target: 'http://localhost:8080',
        target: `http://192.168.20.13:10001`,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/dev-api/, ''),
      },
    }
  },
  // 打包配置
  build: {
    sourcemap: false,  // 构建后是否生成 source map 文件
    outDir: 'dist',  // 指定输出路径
    assetsDir: 'static',  // 指定生成静态资源的存放路径
  },
  // css配置
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: '@import "@/assets/style/mixin.scss"; @import "@/assets/style/function.scss";'
      }
    }
  }
})
